package forum

import org.springframework.dao.DataIntegrityViolationException


class AntwortController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def index() {
        redirect(action: "list", params: params)
    }

    def list(Integer max, Long id) {
		println id
        params.max = Math.min(max ?: 10, 100)
		def query = "from Antwort as b where b.frage="+id+" order by b.dateCreated"
		def antworte = Antwort.findAll(query)
//        [antwortInstanceList: Antwort.list(params), antwortInstanceTotal: Antwort.count()]
        [antwortInstanceList: antworte, antwortInstanceTotal: Antwort.count(), frageId:id]
    }

    def create() {
        [antwortInstance: new Antwort(params)]
    }

//    def save() {
//		def f = Frage.get(params.frage)
//		params.frage=f
//		def ben = Benutzer.get(params.benutzer)
//		params.benutzer=ben
//		
//        def antwortInstance = new Antwort(params)
//        if (!antwortInstance.save(flush: true)) {
//            render(view: "create", model: [antwortInstance: antwortInstance])
//            return
//        }
//		params.frage=params.frage.id
//		session.frage=f
//        flash.message = message(code: 'default.created.message', args: [message(code: 'antwort.label', default: 'Antwort'), antwortInstance.id])
////        redirect(action: "alle", id: antwortInstance.id)
//        redirect(action: "alle", frage: f.id)
//    }
    def save() {
//    	def f = Frage.get(params.frage)
//    			params.frage=f
    			def antwortInstance = new Antwort(params)
    	if (!antwortInstance.save(flush: true)) {
    		render(view: "create", model: [antwortInstance: antwortInstance])
    		return
    	}
    	
    	flash.message = message(code: 'default.created.message', args: [message(code: 'antwort.label', default: 'Antwort'), antwortInstance.id])
    			redirect(action: "list", id: antwortInstance.frage.id)
//    			redirect(action: "list", id: antwortInstance.id)
    }

    def show(Long id) {
        def antwortInstance = Antwort.get(id)
        if (!antwortInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'antwort.label', default: 'Antwort'), id])
            redirect(action: "list")
            return
        }

        [antwortInstance: antwortInstance]
    }

    def edit(Long id) {
        def antwortInstance = Antwort.get(id)
        if (!antwortInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'antwort.label', default: 'Antwort'), id])
            redirect(action: "list")
            return
        }

        [antwortInstance: antwortInstance]
    }

    def update(Long id, Long version) {
        def antwortInstance = Antwort.get(id)
        if (!antwortInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'antwort.label', default: 'Antwort'), id])
            redirect(action: "list")
            return
        }

        if (version != null) {
            if (antwortInstance.version > version) {
                antwortInstance.errors.rejectValue("version", "default.optimistic.locking.failure",
                          [message(code: 'antwort.label', default: 'Antwort')] as Object[],
                          "Another user has updated this Antwort while you were editing")
                render(view: "edit", model: [antwortInstance: antwortInstance])
                return
            }
        }

        antwortInstance.properties = params

        if (!antwortInstance.save(flush: true)) {
            render(view: "edit", model: [antwortInstance: antwortInstance])
            return
        }

        flash.message = message(code: 'default.updated.message', args: [message(code: 'antwort.label', default: 'Antwort'), antwortInstance.id])
        redirect(action: "show", id: antwortInstance.id)
    }

    def delete(Long id) {
        def antwortInstance = Antwort.get(id)
        if (!antwortInstance) {
            flash.message = message(code: 'default.not.found.message', args: [message(code: 'antwort.label', default: 'Antwort'), id])
            redirect(action: "list")
            return
        }

        try {
            antwortInstance.delete(flush: true)
            flash.message = message(code: 'default.deleted.message', args: [message(code: 'antwort.label', default: 'Antwort'), id])
            redirect(action: "list")
        }
        catch (DataIntegrityViolationException e) {
            flash.message = message(code: 'default.not.deleted.message', args: [message(code: 'antwort.label', default: 'Antwort'), id])
            redirect(action: "show", id: id)
        }
    }
//	@Secured("hasRole('ROLE_ADMIN')")
	def alle(){
		session.fId=params.frage
		def fra = Frage.get(params.frage)
		if(!fra)
			fra= Frage.get(session.frage.id)
		println "allw --->> :"+fra
		println  " "+params
//		def antworte = Antwort.findAllWhere(frage:fra)
		def query = "from Antwort as b where b.frage="+fra.id+" order by b.dateCreated"
//		def antworte = Antwort.findAll(sort:'dateCreated'){frage:fra}
		def antworte = Antwort.findAll(query)
		println antworte
		println Antwort.list()
		println "allw --->> :"
		[ant:antworte, frage:fra, user:getAuthenticatedUser()]
	}

	def remoteSave(){
		println params
		 def antwortInstance = new Antwort(params).save(flush:true)
//        if (!antwortInstance.save(flush: true)) {
//            render(view: "create", model: [antwortInstance: antwortInstance])
//            return
//        }

//        flash.message = message(code: 'default.created.message', args: [message(code: 'antwort.label', default: 'Antwort'), antwortInstance.id])
////		redirect action:"alle"
//        redirect(action: "show", id: antwortInstance.id)
		 [ant:Antwort.list()]
	}
}
